app.controller('managerController', ['$scope','ajax','$state', function ($scope,ajax,$state) {
    //初始化页面
    ajax.get({
        url: 'back/manager/page',
        data:{
            state:1,
            currentPage:1,
            pageSize:10
        }
    }).then(function (result) {
        $scope.manager=result.resultData;
    })

    //新增管理员
    $scope.addManager=function(){
        //查询用户所有角色
        ajax.get({
            url: 'back/mRole/list',
        }).then(function (result) {
            $scope.initRole=result;
        })

    }


    $scope.managerDetail={
        managerId:'',
        username:'',
        password:'',
        realName:'',
        phone:'',
        email:"",
        roleIds:1
    }
    //提交
    $scope.submitManager=function(roleIds,managerId){
        console.log($scope.managerDetail);
        if($scope.managerDetail.managerId!==''){
            ajax.post({
                url: 'back/manager/update',
                data:$scope.managerDetail
            }).then(function (result) {
                $state.go('admin.manager',null,{
                    reload:true
                });
            })
            return
        }
        ajax.post({
            url: 'back/manager/add',
            data:$scope.managerDetail
        }).then(function (result) {
            $state.go('admin.manager',null,{
                reload:true
            });
        })


    }


    //编辑管理员
    $scope.updateManager=function(managerId){
        ajax.get({
            url: 'back/mRole/list',
        }).then(function (result) {
            $scope.initRole=result;

            ajax.get({
                url: 'back/manager/query',
                data:{
                    managerId:managerId
                }
            }).then(function (result) {
                $scope.managerDetail=result;
                $scope.managerDetail.roleIds=1;
            })
        })
    }
    //删除管理员
    $scope.deleteManager=function(managerId){
        ajax.post({
            url: 'back/manager/delete',
            data:{
                managerId:managerId
            }
        }).then(function (result) {
            $state.go('admin.manager',null,{
                reload:true
            });
        })

    }



}]);